<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
    <el-form-item label="第一规格数量" prop="unFir">
      <el-input-number v-model="dataForm.unFir"  :min="0"  label="第一规格数量"></el-input-number>
    </el-form-item>
    <el-form-item label="第一规格名称" prop="unFirName">
      <el-input v-model="dataForm.unFirName" placeholder="第一规格名称"></el-input>
    </el-form-item>
    <el-form-item label="第二规格数量" prop="unSed">
      <el-input-number v-model="dataForm.unSed"  :min="0"  label="第二规格数量"></el-input-number>
    </el-form-item>
    <el-form-item label="第二规格名称" prop="unSedName">
      <el-input v-model="dataForm.unSedName" placeholder="第二规格名称"></el-input>
    </el-form-item>
    <el-form-item label="第三规格数量" prop="unThr">
      <el-input-number v-model="dataForm.unThr"  :min="0"  label="第三规格数量"></el-input-number>
    </el-form-item>
    <el-form-item label="第三规格名称" prop="unThrName">
      <el-input v-model="dataForm.unThrName" placeholder="第三规格名称"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          unFir: '',
          unFirName: '',
          unSed: '',
          unSedName: '',
          unThr: '',
          unThrName: ''
        },
        dataRule: {
          unFir: [
            { required: true, message: '第一规格数量不能为空', trigger: 'blur' }
          ],
          unFirName: [
            { required: true, message: '第一规格名称不能为空', trigger: 'blur' }
          ]
          // unSed: [
          //   { required: true, message: '第二规格数量不能为空', trigger: 'blur' }
          // ],
          // unSedName: [
          //   { required: true, message: '第二规格名称不能为空', trigger: 'blur' }
          // ]
          // unThr: [
          //   { required: true, message: '第三规格数量不能为空', trigger: 'blur' }
          // ],
          // unThrName: [
          //   { required: true, message: '第三规格名称不能为空', trigger: 'blur' }
          // ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/generator/spec/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.unFir = data.spec.unFir
                this.dataForm.unFirName = data.spec.unFirName
                this.dataForm.unSed = data.spec.unSed
                this.dataForm.unSedName = data.spec.unSedName
                this.dataForm.unThr = data.spec.unThr
                this.dataForm.unThrName = data.spec.unThrName
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/spec/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'unFir': this.dataForm.unFir,
                'unFirName': this.dataForm.unFirName,
                'unSed': this.dataForm.unSed,
                'unSedName': this.dataForm.unSedName,
                'unThr': this.dataForm.unThr,
                'unThrName': this.dataForm.unThrName
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
